/**
 * Copyright (C), 2020, XXX有限公司
 * FileName: UserDetailsServiceImpl
 * Author:   Administrator
 * Date:     2020/5/9 14:57
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.fmyexample.v2mcsecuritybootjdbc.sucerity;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fmyexample.v2mcsecuritybootjdbc.modular.model.User;
import com.fmyexample.v2mcsecuritybootjdbc.modular.service.IUserService;
import lombok.AllArgsConstructor;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 〈一句话功能简述〉<br> 
 * 〈〉
 *
 * @author Administrator
 * @create 2020/5/9
 * @since 1.0.0
 */
@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

    private final IUserService userService;



    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        User user = userService.getOne(Wrappers.<User>query().eq(User.USER_PHONE, username));
        if(user==null){
            throw new UsernameNotFoundException("用户不存在！");
        }
        //user.setPassword(ToolKit.md5Salt(user.getUserSalt(),"");
        CuUserDetails userDetails = new CuUserDetails();
        BeanUtil.copyProperties(user, userDetails);
       // SucerityConfig.SALT_HOLDER.set(user.getUserSalt());
        return  userDetails;
    }
}